package com.anzh.viewpager;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnClickListener, OnPageChangeListener{
	 private ViewPager vp;
	    private ViewPagerAdapter vpAdapter;
	    private List<View> views;
	    private int lastValue = -1;
	    
	    //引导图片资源
	    private String[] pics;
	    
	    //底部小点图片
	    private ImageView[] dots ;
	    
	    //记录当前选中位置
	    private int currentIndex;
	    private final int UPDATEDATA = 1;
	    
	    Handler mHandler = new Handler(){
	        public void handleMessage(Message msg) {
	            switch (msg.what) {
                    case UPDATEDATA:
                        BitmapData bitmapData = msg.getData().getParcelable("bitmap");
                        bitmapData.imageView.setImageBitmap(bitmapData.bitmap);
                        break;
                }
	        };
	    };
	    
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        views = new ArrayList<View>();
	        initImage();
	        vp = (ViewPager) findViewById(R.id.viewpager);
	        //初始化Adapter
	        vpAdapter = new ViewPagerAdapter(views);
	        vp.setAdapter(vpAdapter);
	        //绑定回调
	        vp.setOnPageChangeListener(this);
	        //初始化底部小点
	        initDots();
	        
	    }
	    private void initImage(){
	        try {
                pics = this.getResources().getAssets().list("guide");
                LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                
                //初始化引导图片列表
                for(int i=0; i<pics.length; i++) {
                    ImageView iv = new ImageView(this);
                    iv.setLayoutParams(mParams);
                    GetBitMap.instance().add(this, mHandler,"guide/"+pics[i], UPDATEDATA,iv);
                    views.add(iv);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
	    }
	    
	    
	    private void initDots() {
	        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
	        mLayoutParams.setMargins(3, 0, 3, 0);
	        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
	        dots = new ImageView[pics.length];
	        //循环取得小点图片
	        for (int i = 0; i < pics.length; i++) {
	            dots[i] = new ImageView(this);
	            dots[i].setLayoutParams(mLayoutParams);
                dots[i].setBackgroundResource(R.drawable.dot);
	            dots[i].setEnabled(true);//都设为灰色
	            dots[i].setOnClickListener(this);
	            dots[i].setTag(i);//设置位置tag，方便取出与当前位置对应
	            ll.addView(dots[i]);
	        }

	        currentIndex = 0;
	        dots[currentIndex].setEnabled(false);//设置为白色，即选中状态״̬
	    }
	    
	    /**
	     *设置当前的引导页
	     */
	    private void setCurView(int position)
	    {
	        if (position < 0 || position >= pics.length) {
	            return;
	        }

	        vp.setCurrentItem(position);
	    }

	    /**
	     *这只当前引导小点的选中
	     */
	    private void setCurDot(int positon)
	    {
	        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
	            return;
	        }

	        dots[positon].setEnabled(false);
	        dots[currentIndex].setEnabled(true);

	        currentIndex = positon;
	    }

	    //当滑动状态改变时调用
	    @Override
	    public void onPageScrollStateChanged(int arg0) {
          if(arg0 == 0){
        	   if(lastValue == pics.length-1){
        		   Toast.makeText(this, "已经是最后一张了", Toast.LENGTH_SHORT).show();
        	   }
           }	        
	    }

	    //当前页面被滑动时调用
	    @Override
	    public void onPageScrolled(int arg0, float arg1, int arg2) {
	    	lastValue = arg0;
	    }

	    //当新的页面被选中时调用
	    @Override
	    public void onPageSelected(int arg0) {
	        //设置底部小点选中状态״̬
	        setCurDot(arg0);
	    }

	    @Override
	    public void onClick(View v) {
	        int position = (Integer)v.getTag();
	        setCurView(position);
	        setCurDot(position);
	    }
}